package com.fzdy.dataplatform.core.service.impl;

import com.fzdy.dataplatform.core.dao.Dao;
import com.fzdy.dataplatform.core.exception.CheckException;
import com.fzdy.dataplatform.core.service.Service;
import com.fzdy.dataplatform.core.vo.Page;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/27 0027.
 */
@Transactional
public abstract class BaseServiceImpl<T> implements Service<T> {
    public BaseServiceImpl() {
    }

    public T get(Serializable id) {
        return id == null?null: (T) this.getDao().get(id);
    }

    protected abstract Dao getDao();

    public T load(Serializable id) {
        return (T) this.getDao().load(id);
    }

    public void queryEq(T entity, Page<T> page) {
        List list = this.getDao().findByExampleEq(entity, page.getPageCurrent(), page.getPageSize());
        long count = this.getDao().countByExampleEq(entity);
        page.setTotalRow(Long.valueOf(count));
        page.setList(list);
    }

    public List<T> queryEq(T entity) {
        return this.getDao().findByExampleEq(entity);
    }

    public Serializable insertT(T t) {
        return this.getDao().save(t);
    }

    public void updateT(T t) {
        this.getDao().update(t);
    }

    public List<T> getList() {
        return this.getDao().findAll();
    }

    public void deleteByIdT(Serializable id) {
        this.getDao().deleteT(id);
    }

    public void deleteAllT() {
    }

    public long count() {
        return (long)this.getDao().count();
    }

    protected void checkNull(Object obj, String msg) {
        if(obj == null) {
            throw new CheckException(msg);
        }
    }

    protected void checkString(String str, String msg) {
        this.checkNull(str, msg);
        if(str.trim().isEmpty()) {
            throw new CheckException(msg);
        }
    }

    protected void checkTime(long time1, long time2, String msg) {
        if(time1 > time2) {
            throw new CheckException(msg);
        }
    }

    protected void checkPen(Float pensionPrice, String msg) {
        if(pensionPrice.floatValue() < 0.0F) {
            throw new CheckException(msg);
        }
    }

    protected void errorMsg(String msg) {
        throw new CheckException(msg);
    }



    public boolean has(T t) {
        return this.getDao().has(t);
    }

    public List<T> findByExampleLike(T example) {
        return this.getDao().findByExampleLike(example);
    }

    public List<T> findByExampleEq(T example) {
        return this.getDao().findByExampleEq(example);
    }
    public List<T> findByExampleEq(T example,String order) {
        return this.getDao().findByExampleEq(example,order);
    }
    public void getPageByExampleLike(T query, Page<T> page) {
        List<T> list = this.getDao().findByExampleLike(query, page.getPageCurrent(), page.getPageSize());
        long count = this.getDao().countByExampleLike(query);
        page.setList(list);
        page.setTotalRow(Long.valueOf(count));
    }

    public void getPageByExampleEq(T query, Page<T> page) {
        List<T> list = this.getDao().findByExampleEq(query, page.getPageCurrent(), page.getPageSize());
        long count = this.getDao().countByExampleEq(query);
        page.setList(list);
        page.setTotalRow(Long.valueOf(count));
    }

    public void getPageByExampleNe(T query, Page<T> page, Map<String, Object> neqProperty) {
        List<T> list = this.getDao().findByExampleEqNeProperty(query, page.getPageCurrent(), page.getPageSize(), neqProperty);
        long count = this.getDao().countByExampleEqNeProperty(query, neqProperty);
        page.setList(list);
        page.setTotalRow(Long.valueOf(count));
    }
    public void batchDelete(Serializable[] ids){
        this.getDao().batchDelete(ids);
    }
}
